package com.loyal.kit;

import android.content.Context;
import android.text.TextUtils;
import android.util.Base64;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;

public class ResUtil {
    /**
     * @param variableName video
     * @param c            mipmap.class
     */
    public static int getResId(String variableName, Class<?> c) {
        try {
            Field idField = c.getDeclaredField(variableName);
            return idField.getInt(idField);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * @param fileName "json/index.json"
     */
    public static InputStream openAsset(Context context, String fileName) throws IOException {
        return context.getAssets().open(fileName);
    }

    /**
     * @param fileName assets下的文件路径地址
     */
    public static String assetsFile2String(Context context, String fileName) {
        return assetsFile2String(context, fileName, "utf-8");
    }

    public static String assetsFile2String(Context context, String fileName, String charsetName) {
        try (InputStream is = openAsset(context, fileName)) {
            return StreamHelper.toString(is, charsetName);
        } catch (IOException e) {
            return "";
        }
    }

    /**
     * @param spinner 下拉控件
     * @return 获取下拉列表当前属性值
     */
    public static @Nullable
    Object getSpinSelectedItemValue(Spinner spinner) {
        if (null == spinner)
            return null;
        return spinner.getSelectedItem();
    }

    /**
     * @param spinner 下拉控件
     * @return 获取下拉列表当前属性值
     */
    public static @Nullable
    String getSpinSelectedItemValue(Spinner spinner, @NonNull String methodName) {
        if (null == spinner || TextUtils.isEmpty(methodName))
            return "";
        return (String) getMethodValue(spinner.getSelectedItem(), methodName);
    }

    /**
     * @param tClass     xxx.class
     * @param methodName methodName
     * @return 此类该方法的返回值
     */
    public static @Nullable
    Object getMethodValue(Object tClass, String methodName) {
        if (null == tClass)
            return null;
        try {
            Method method = tClass.getClass().getMethod(methodName);
            return method.invoke(tClass);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*public static Object setMethodParam(Class<?> tClass, String methodName) {
        if (null == tClass)
            return null;
        try {
            //Class<?>... paramClass
            //Object... params
            Method method = tClass.getClass().getMethod(methodName, paramClass);
            return method.invoke(tClass, params);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }*/

    public static @NonNull
    String replaceText(CharSequence sequence) {
        return (String) replace(sequence, "");
    }

    public static @NonNull <T> T replace(T source, @NonNull T replace) {
        return null == source ? replace : source;
    }

    public static String encode2Utf8(@Nullable String str) {
        return encodeString(str, "utf-8");
    }

    public static String encodeString(@Nullable String str,@NonNull String enc) {
        if (TextUtils.isEmpty(str))
            return "";
        try {
            return URLEncoder.encode(str, enc);
        } catch (Exception e) {
            return str;
        }
    }

    public static String decode2Utf8(String str) {
        return decodeString(str, "utf-8");
    }

    public static String decodeString(String str, String enc) {
        if (TextUtils.isEmpty(str))
            return "";
        try {
            return URLDecoder.decode(str, enc);
        } catch (Exception e) {
            return str;
        }
    }

    /**
     * base64加密字符串
     */
    public static String base64Encode(String source) {
        if (TextUtils.isEmpty(source))
            return "";
        return Base64.encodeToString(source.getBytes(), Base64.DEFAULT);
    }

    /**
     * base64加密字符串
     */
    public static String base64Encode(String source, Charset charset) {
        if (TextUtils.isEmpty(source))
            return "";
        return Base64.encodeToString(source.getBytes(charset), Base64.DEFAULT);
    }

    /**
     * base64解密字符串
     */
    public static String base64Decode(String source) {
        if (TextUtils.isEmpty(source))
            return "";
        byte[] decodeBytes = Base64.decode(source, Base64.DEFAULT);
        return new String(decodeBytes);
    }

    /**
     * base64解密字符串
     */
    public static String base64Decode(String source, Charset charset) {
        if (TextUtils.isEmpty(source))
            return "";
        byte[] decodeBytes = Base64.decode(source, Base64.DEFAULT);
        return new String(decodeBytes, charset);
    }

}
